#include <raylib.h>
#include <raymath.h>
#include <scene.h>
#include <vector>
#include <tilemap.h>
#include <raycast.h>

class SceneEast : public Scene {
private:
    Vector2 curser;
    float SCALE = 48 * 3;
    RayCast rayCaster;
    
    // SHADER
    // Image mask_image = GenImageColor(10 * 48, 10* 48, Color{0,0,0,0});
    // Texture2D mask_texture;
    // Shader maskShader;
    // int pointCount = 360;
    // int mask_resolutionLoc;
    // int mask_pointCountLoc;
    // std::vector<Vector2> ps = {Vector2{300,100},Vector2{180,400},Vector2{380,400} ,Vector2{400,300}};

    Camera2D camera;
public:
    SceneEast() {
        
    }
    // SceneEast(TileMap map)
    // {
    //     rayCaster.setWalls(map.walls);

    //     // SHADER
    //     mask_texture = LoadTextureFromImage(mask_image);
    //     maskShader = LoadShader(0,"assets/shaders/mask.fs");
    //     mask_resolutionLoc = GetShaderLocation(maskShader, "resolution");
    //     mask_pointCountLoc = GetShaderLocation(maskShader, "pointCount");
    //     float resolution[2] = { (float)10*48, (float)10*48 };
    //     SetShaderValue(maskShader, mask_resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
    //     // 设置顶点数量
    //     SetShaderValue(maskShader, mask_pointCountLoc, &pointCount, SHADER_UNIFORM_INT);
    // }
    
    ~SceneEast() {}

    void init() override {
        TileMap tilemap;
        tilemap.load("assets\\maze0.txt");
        rayCaster.setWalls(tilemap.walls);

        // SHADER
        // mask_texture = LoadTextureFromImage(mask_image);
        // maskShader = LoadShader(0,"assets/shaders/mask.fs");
        // mask_resolutionLoc = GetShaderLocation(maskShader, "resolution");
        // mask_pointCountLoc = GetShaderLocation(maskShader, "pointCount");
        // float resolution[2] = { (float)10*48, (float)10*48 };
        // SetShaderValue(maskShader, mask_resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
        // // 设置顶点数量
        // SetShaderValue(maskShader, mask_pointCountLoc, &pointCount, SHADER_UNIFORM_INT);

        camera.target = {0,0};
        camera.offset = {0,0};
        camera.rotation = 0.0f;
        camera.zoom = 3.0f * 48;
    }

    void render(float dt) override {
        ClearBackground(BLACK);
        curser = GetMousePosition();

        rayCaster.cast(Vector2Scale(curser, (float)1/48/3));

        
        BeginMode2D(camera);
            DrawTriangleFan(rayCaster.polygonPoints.data() , rayCaster.polygonPoints.size(), Color{20,120,0,255});
        EndMode2D();

        if(IsKeyPressed(KEY_SPACE)){
            std::cout<<"";
        }

        // 绘制墙壁
        for (auto& wall : rayCaster.walls)
            DrawLineV(Vector2Scale(wall.first,SCALE), Vector2Scale(wall.second,SCALE), WHITE);

        BeginMode2D(camera);
        for(int i=0; i<rayCaster.polygonPoints.size(); i++)
        {
            DrawLineV(curser/camera.zoom, Vector2Scale(rayCaster.polygonPoints[i],1), WHITE);
        }
        EndMode2D();

        // BeginShaderMode(maskShader);
        // // // 更新顶点数据到着色器
        // pointCount = rayCaster.polygonPoints.size();
        // SetShaderValue(maskShader, mask_pointCountLoc, &pointCount, SHADER_UNIFORM_INT);
        // for (int i = 0; i < pointCount; i++) {
        //     char pointName[32];
        //     sprintf(pointName, "points[%d]", i);
        //     int pointLoc = GetShaderLocation(maskShader, pointName);
        //     if(i < rayCaster.polygonPoints.size()){
        //         float pointValue[2] = { rayCaster.polygonPoints[i].x *48, rayCaster.polygonPoints[i].y *48};
        //         SetShaderValue(maskShader, pointLoc, pointValue, SHADER_UNIFORM_VEC2);
        //     }
            
        // }
        // // // // 绘制应用了着色器的渲染纹理
        // DrawTextureEx(mask_texture, {0,0}, 0, 3,WHITE);
        // EndShaderMode();

        

        for(int i=0; i<rayCaster.polygonPoints.size(); i++)
            DrawText(TextFormat("%d",i), rayCaster.polygonPoints[i].x*SCALE, rayCaster.polygonPoints[i].y*SCALE, 32, RED);

        for(auto& i : rayCaster.usefulIntersection) if(i!=nullptr) delete i;
        rayCaster.usefulIntersection.clear();
    }
};